<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/09/14 17:37:28 $)
// $RCSfile: functions_clienttickets.php,v $ : $Revision: 1.9 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Builds a Department list for the current user group
*/
function buildGroupDepartmentList()
{
	global $_SWIFT, $dbCore;

	$departmentlist = array();
	$dbCore->query("SELECT departments.* FROM `". TABLE_PREFIX ."departments` AS departments LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (departments.departmentid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_DEPARTMENT ."') WHERE tgroupassigns.tgroupid = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."' ORDER BY departments.displayorder ASC;");
	while ($dbCore->nextRecord())
	{
		if ($dbCore->Record["departmentmodule"] == MODULE_TICKETS && $dbCore->Record["departmenttype"] == SWIFTPUBLIC)
		{
			$departmentlist[$dbCore->Record["departmentid"]] = $dbCore->Record;
		}
	}

	return $departmentlist;
}

/**
* Builds a Priority list for the current user group
*/
function buildGroupPriorityList()
{
	global $_SWIFT, $dbCore;

	$prioritylist = array();

	$dbCore->query("SELECT ticketpriorities.* FROM `". TABLE_PREFIX ."ticketpriorities` AS ticketpriorities LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (ticketpriorities.priorityid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_PRIORITY ."') WHERE tgroupassigns.tgroupid = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."' ORDER BY ticketpriorities.displayorder ASC;");
	while ($dbCore->nextRecord())
	{
		if ($dbCore->Record["type"] == SWIFTPUBLIC)
		{
			$prioritylist[$dbCore->Record["priorityid"]] = $dbCore->Record;
		}
	}

	return $prioritylist;
}

/**
* Get email list based on user type
*/
function getUserEmailList()
{
	global $_SWIFT, $dbCore;

	$_emaillist = array();

	if ($_SWIFT["user"]["ismanager"] == 1)
	{
		$_useridlist = array();
		$dbCore->query("SELECT `userid` FROM `". TABLE_PREFIX ."users` WHERE `usergroupid` = '". intval($_SWIFT["user"]["usergroupid"]) ."';");
		while ($dbCore->nextRecord())
		{
			$_useridlist[] = strtolower($dbCore->Record["userid"]);
		}

		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."useremails` WHERE `userid` IN (". buildIN($_useridlist) .");");
		while ($dbCore->nextRecord())
		{
			$_emaillist[] = strtolower($dbCore->Record["email"]);
		}
	} else {
		if (is_array($_SWIFT["user"]["email"]))
		{
			foreach ($_SWIFT["user"]["email"] as $key=>$val)
			{
				$_emaillist[] = strtolower($val);
			}
		} else {
			$_emaillist = array(strtolower($_SWIFT["user"]["email"]));
		}
	}

	return $_emaillist;
}


if(!function_exists('str_split')){ 
	function str_split($string,$split_length=1){ 
		$count = strlen($string);
		if($split_length < 1){
			return false;
		} elseif($split_length > $count){
			return array($string);
		} else {
			$num = (int)ceil($count/$split_length);
			$ret = array();
			for($i=0;$i<$num;$i++){
				$ret[] = substr($string,$i*$split_length,$split_length);
			}
			return $ret;
		}
	}
}

/**
* Explode a string with a set of multiple delimiters
*/
function splitRecipients($delimiters,$str)
{
	$del = str_split($delimiters);
	$ret = array($str);

	foreach($del as $cur) {
		$ret2 = $ret;
		unset($ret);

		foreach($ret2 as $s) {
			$t = explode($cur,$s);

			foreach($t as $n) {
				if(!empty($n) && isValidEmail($n)) {
					$ret[] = trim($n);
				}
			}
		}
	}

	return $ret;
}

?>